package filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;

import Impl.UserServiceImpl;
import listeners.ContextListener;

/**
 * Servlet Filter implementation class MainFilter
 */
public class MainFilter implements Filter {
	UserServiceImpl userService;
    /**
     * Default constructor. 
     */
    public MainFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		request.setCharacterEncoding("UTF-8");
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse res=(HttpServletResponse)response;
		// pass the request along the filter chain
		if(req.getRequestURI().endsWith("checklogin")) {
			String userid=(String)req.getParameter("account");
			String pwd=(String)req.getParameter("password");
			int identity=Integer.parseInt(req.getParameter("identity"));
			int user_type=1;
			if(identity==2)user_type=2;
			else if(identity==3)user_type=3;
			try {
				String username=userService.checkuser(userid, pwd,user_type);
				req.getSession().setAttribute("userid", userid);
				req.getSession().setAttribute("identity", identity);
				if(identity==1) {
					//res.sendRedirect("index/index.jsp");
					req.getRequestDispatcher("index/index.jsp").forward(request, response);
				}
					//req.getRequestDispatcher("index/index.jsp").forward(request, response);	
				if(identity==2||identity==3)res.sendRedirect("manage/index.jsp");
				return;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				req.getSession().setAttribute("errmsg", e.getMessage());
				req.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
			}			
		}
		if(req.getRequestURI().endsWith("index")) {
			req.getRequestDispatcher("jsp/index.jsp").forward(request, response);
			return;
		}
		if(req.getRequestURI().endsWith("exit")) {
			req.getSession().invalidate();
			//res.sendRedirect("index/index.jsp");
			req.getRequestDispatcher("index/index.jsp").forward(request, response);
			return;
		}
		if(req.getRequestURI().endsWith("UserAdd")) {
			String userid=(String)req.getParameter("account");
			String pwd=(String)req.getParameter("password");
			String username=(String)req.getParameter("selectedname");
			try {
				this.userService.useradd(userid, pwd, username);		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				req.getRequestDispatcher("register").forward(request, response);
				e.printStackTrace();
			} 
			req.getRequestDispatcher("enter").forward(request, response);	
			return;
		}
		String userid=(String)req.getSession().getAttribute("userid");
		if(userid==null) {
			//req.getRequestDispatcher("index/index.jsp").forward(request, response);
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub

		try {
			ApplicationContext applicationContext=(ApplicationContext)fConfig.getServletContext().getAttribute("ctx");
			userService=(UserServiceImpl)applicationContext.getBean("UserServiceImpl");	
		}catch(NullPointerException ex) {
		}
	}

}
